package com.china08.yxyapi.api.manage.tec.teaching.zujuan.editcomponent;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 教育局平台办公 编辑试卷  换题
 * 
 */
//@Api(tags = { "v53", "编辑试卷  换题" })
@CrossOrigin
@RestController
public class EditComponentChangeQuesController extends YxyApiBaseController{
	
	@Autowired
	private ElasticQuesService elasticQuesService;
	
//	@ApiOperation(value = "编辑试卷  换题")
//	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/editcomponent/change/ques", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
		RespModel model=new RespModel();
		List<QuesModel> list = reqModel.list;
		QuesModel oldQuesModel = reqModel.ques;
		// 所有的题ids
		List<String> quesIdList = list.stream().map(x -> x.quesId)
                 .collect(Collectors.toList());
        if (oldQuesModel.quesId == null) {
        	Assert.isTrue(oldQuesModel.quesId!=null, "您操作过于频繁,请稍候再操作!");
        	return model;
		}
		//原始题
		ElasticYxyYxyQuesResource oldQues= elasticQuesService.findByEsQuesId(oldQuesModel.quesId);
		String difficultLevel = "";
		if (oldQues!=null) {
			difficultLevel = oldQues.getDifficultyLevel();
		}
		ElasticYxyYxyQuesResource elasticYxyYxyQuesResource = elasticQuesService.findByQuestionTypeAndQuesIdIn(oldQues.getQuestionType(), oldQuesModel.know, difficultLevel, quesIdList);
		
		Assert.isTrue(elasticYxyYxyQuesResource!=null, "没有题可换了!");
		QuestionRespModel questionRespModel=new QuestionRespModel();
		//试题id
		questionRespModel.quesId = elasticYxyYxyQuesResource==null?"":elasticYxyYxyQuesResource.getId();
		//题干
		questionRespModel.quesBody = elasticYxyYxyQuesResource==null?"": elasticYxyYxyQuesResource.getQuesBody();
		model.QuestionRespModel = questionRespModel;
		return model;
	}


	protected static class ReqModel extends YxyApiReqModel {
		//要换的题
		public QuesModel ques;
		
		public List<QuesModel> list;
	}
	
	protected static class QuesModel {
		//试题的id
		public String quesId;
		//所有的知识点id
		public List<Integer> know;
		// 题型
//		public String quesType;
		//分数
//		public Integer score;
		
	}
	protected static class RespModel extends YxyApiRespModel {
        public QuestionRespModel QuestionRespModel;
    }
	protected static class QuestionRespModel {
        // 题id
        public String quesId;
        // 题干
        public Object quesBody;
        // 分数
        public Integer point = 0;
    }
//	protected static class QuesRespModel {
//	}
	
}
